﻿#include "TCPServerDlg.h"
#include "Note.h"
#include "Application.h"

TCPServerDlg::TCPServerDlg(QWidget *parent, bool is_child):
	BaseDlg(parent,"TCP服务端通道"),
	content{},
	m_bNewConnInfo(true)
{
	if(is_child)
	{
		ui.setupUi(this);
	}
	else
	{
		ui.setupUi(&content);
		base_ui.content->addWidget(&this->content);
	}

	ui.comboBox_RecvDataName->clear();
	ui.comboBox_SendDataName->clear();
	for(auto& item : Application::Instance().cfg.DataTypes())
	{
		ui.comboBox_RecvDataName->addItem(item.second.c_str(), item.first);
		ui.comboBox_SendDataName->addItem(item.second.c_str(), item.first);
	}

	connect(ui.checkBox, &QCheckBox::toggled, this, &TCPServerDlg::togglog_sync);
	connect(ui.comboBox_RecvDataName, &QComboBox::currentIndexChanged, this, &TCPServerDlg::RecvDataChanged);

	//connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(slotApply()));
	//connect(ui.checkBox, SIGNAL(clicked()), this, SLOT(slotCheck()));
}

TCPServerDlg::~TCPServerDlg()
{
}
bool TCPServerDlg::get_info(ChannelCreateInfo& info)
{
	info.type |= CHANNEL_PROTO_TYPE_TCP_SERVER;

	auto title = ui.lineEdit_ChannName->text();
	if(title.isEmpty())
	{
		Note nt{ "通道名称不能为空！" };
		nt.showDialog();
		return false;
	}
	strcpy(info.title, title.toStdString().c_str());
	struct
	{
		bool is_ok;
		unsigned int key;
	}data_pair[2]{};

	data_pair[0].key = ui.comboBox_RecvDataName->currentData().toUInt(&data_pair[0].is_ok);
	data_pair[1].key = ui.comboBox_SendDataName->currentData().toUInt(&data_pair[1].is_ok);

	if(!(data_pair[0].is_ok && data_pair[1].is_ok))
	{
		Note nt{ "收发数据类型异常！" };
		nt.showDialog();
		return false;
	}

	info.input_type = data_pair[0].key;
	info.output_type = data_pair[1].key;

	QString local_ip = ui.comboBox_LocalIP->currentText();
	auto local_port = ui.spinBox_LocalPort->value();

	if(local_port != 0)
	{
		info.input_address.init(local_ip.toStdString().c_str(), local_port);
	}

	return true;
}


void TCPServerDlg::RecvDataChanged(int index)
{
	if(ui.checkBox->isChecked())
	{
		ui.comboBox_SendDataName->setCurrentIndex(index);
	}
}

void TCPServerDlg::togglog_sync(bool b)
{
	ui.comboBox_SendDataName->setEnabled(!b);
	if(b)
	{
		ui.comboBox_SendDataName->setCurrentIndex(ui.comboBox_RecvDataName->currentIndex());
	}
}

//
//void TCPServerDlg::slotCheck()
//{
//	/*if (ui.checkBox->isChecked())
//	{
//		ui.comboBox_SendDataName->setEnabled(false);
//		ui.comboBox_SendDataName->setCurrentIndex(ui.comboBox_RecvDataName->currentIndex());
//	}
//	else
//		ui.comboBox_SendDataName->setEnabled(true);*/
//}
//
//void TCPServerDlg::slotApply()
//{
//	//ConnInfo info;
//
//	//info.type = ConnType_Server_TCP;
//	//info.chanName = ui.lineEdit_ChannName->text();
//	//info.localIP = ui.comboBox_LocalIP->currentText();
//	//info.localPort = ui.spinBox_LocalPort->text().toInt();
//	//info.recvDataName = ui.comboBox_RecvDataName->currentText();
//	//info.sendDataName = ui.comboBox_SendDataName->currentText();
//
//	////std::vector<UDPSendCgf> lstUDPSendCgf;
//	//if ((m_stConnInfo.localIP != info.localIP || m_stConnInfo.localPort != info.localPort)
//	//	&& !m_bNewConnInfo)
//	//{
//	//	emit sigDisConnect(m_stConnInfo, info);
//	//}
//
//	//emit sigConnInfo(info, m_stConnInfo, m_bNewConnInfo);
//	//m_bNewConnInfo = false;
//	//m_stConnInfo = info;
//
//	//this->hide();
//}